//
//  YUYAMeViewController.m
//  com.yigu.yuya
//
//  Created by hxp on 16/3/2.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIAppSettingViewController.h"

//#import "YAYIPersoninfoViewController.h"
#import "YAYIModifyAccountViewController.h"
#import "YAYINotifySettingViewController.h"
#import "YAYIAboutViewController.h"
#import "YAYIFeedbackViewController.h"
#import "YAYILoginViewController.h"

#import "YAYISettingCell.h"
#import "YAYISettingSectionModel.h"
#import "YAYISettingItemModel.h"


#import "YAYIShareView.h"

//#import "UMSocial.h"

@interface YAYIAppSettingViewController ()

@property (nonatomic, weak) UIView *footerView;
@property (strong,nonatomic) UIButton *backBtn;

// 覆盖层
@property (nonatomic, weak) UIView *maskView;
// 日期选择view
@property (nonatomic, weak) UIView *dateSelectView;

@end

@implementation YAYIAppSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置";
    self.tableView.separatorColor = YAYICellLineColor;
    
    // 添加组
    [self setupSection0];
    [self setupSection1];
    [self setupSection2];
    
    [self setupFooterView];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
//    self.tabBarController.tabBar.hidden = NO;
}

- (void)setupFooterView
{
    UIButton *quitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    quitBtn.backgroundColor = YAYIColor(251, 61, 51);
    
    [quitBtn setTitle:@"退出当前账号" forState:UIControlStateNormal];
    quitBtn.titleLabel.textColor = [UIColor whiteColor];
    [quitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [quitBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    quitBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    quitBtn.layer.cornerRadius = 5;
    quitBtn.layer.masksToBounds = YES;
    
    [self.view addSubview:quitBtn];
    [quitBtn addTarget:self action:@selector(quitClick:) forControlEvents:UIControlEventTouchUpInside];
    
    quitBtn.frame = CGRectMake(20, SCREEN_height - (80 + 44) * YAYIScreenScale, SCREEN_width - 44 * YAYIScreenScale, 44 * YAYIScreenScale);
    
    
}



- (void)setupSection0
{
//    __block typeof(self) weakself = self;
    
    YAYISettingItemModel *item0 = [YAYISettingItemModel itemWithIcon:nil name:@"密码修改"];
    item0.destVcClass = [YAYIModifyAccountViewController class];
    
    item0.executeOperation = ^(NSIndexPath *indexPath){
//        YAYIModifyAccountViewController *editVc = [[YAYIModifyAccountViewController alloc] init];
//        editVc.indexPath = indexPath;
//        [weakself.navigationController pushViewController:editVc animated:YES];
        
    };
    
    YAYISettingSectionModel *section0 = [[YAYISettingSectionModel alloc]init];
    section0.itemArray = @[item0];
    
    [self.sectionArray addObject:section0];

}


- (void)setupSection1
{
    __block typeof(self) weakself = self;
    
    YAYISettingItemModel *item0 = [YAYISettingItemModel itemWithIcon:nil name:@"新消息通知"];
    item0.destVcClass = [YAYINotifySettingViewController class];
    item0.executeOperation = ^(NSIndexPath *indexPath){
        //        YAYINotifySettingViewController *editVc = [[YAYINotifySettingViewController alloc] init];
        //        editVc.indexPath = indexPath;
        //        [weakself.navigationController pushViewController:editVc animated:YES];
        
    };
    
    YAYISettingSectionModel *section1 = [[YAYISettingSectionModel alloc]init];
    section1.itemArray = @[item0];
    
    [self.sectionArray addObject:section1];

}

- (void)setupSection2
{
//     __block typeof(self) weakself = self;
    
    YAYISettingItemModel *item0 = [YAYISettingItemModel itemWithIcon:nil name:@"关于雅同"];
    item0.destVcClass = [YAYIAboutViewController class];
    
//    YAYISettingItemModel *item1 = [YAYISettingItemModel itemWithIcon:nil name:@"推荐给朋友"];
//    item1.executeOperation = ^(NSIndexPath *indexPath){
//        NSLog(@"推荐给朋友");
//        [weakself shareSelect];
//        
//    };
    
    
    YAYISettingItemModel *item2 = [YAYISettingItemModel itemWithIcon:nil name:@"意见反馈"];
    item2.executeOperation = ^(NSIndexPath *indexPath){
//       [[SPKitExample sharedInstance] exampleOpenFeedbackViewController:NO fromViewController:self];
    };
    
    YAYISettingSectionModel *section2 = [[YAYISettingSectionModel alloc]init];
    section2.itemArray = @[item0, item2];
    [self.sectionArray addObject:section2];
}


- (void)shareSelect
{
    YAYIShareView *shareView = [[YAYIShareView alloc] initWithDelegate:self];
    [shareView show];
}


#pragma mark - 按钮相应方法
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)quitClick:(UIButton *)sender
{
    YAYILog(@"quitClick");
    
    [self.navigationController popViewControllerAnimated:NO];
    
    //============删除cookie==============="
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    
    //删除cookie
    for (NSHTTPCookie *tempCookie in cookies) {
        [cookieStorage deleteCookie:tempCookie];
    }
    
    //把cookie打印出来，检测是否已经删除
    NSArray *cookiesAfterDelete = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    for (NSHTTPCookie *tempCookie in cookiesAfterDelete) {
        YAYILog(@"cookieAfterDelete: %@", tempCookie);
    }
    
    // 删除cookie name
    NSUserDefaults *userdef = [NSUserDefaults standardUserDefaults];
    [userdef removeObjectForKey:@"loginCookie"];
    
    
    // 注销im
//    [[SPKitExample sharedInstance] exampleLogout];
    
    // 返回
    [self back];
    
    // 掉起登录接口
    if (self.loginOperation) {
        [YAYILoginViewController loginStateChanged:YAYILoginStateLogout];
        self.loginOperation();
    }
}


- (void)cancelClick:(UIButton *)sender
{
    
    [self.dateSelectView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_bottom).offset(0);
    }];
    
    __block typeof(self) weakself = self;
    [UIView animateWithDuration:0.5 animations:^{
        self.maskView.alpha = 0;
//        self.dateSelectView.alpha = 0.5;
        [self.dateSelectView layoutIfNeeded];
    } completion:^(BOOL finished) {
        [weakself.maskView removeFromSuperview];
        [weakself.dateSelectView removeFromSuperview];
        self.maskView = nil;
        self.dateSelectView = nil;
    }];
}

- (void)maskViewClick:(UITapGestureRecognizer *)sender
{
    
    [self cancelClick:nil];
    
}

#pragma mark - 代理方法
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    // 去除数据模型
//    YAYISettingGroup *goup = self.groups[indexPath.section];
//    YAYISettingItem *item = goup.items[indexPath.row];
//
//    
//    YAYISettingCell *cell = [YAYISettingCell cellWith:tableView];
//    cell.textLabel.textColor = YAYIColor(72, 69, 72);;
//    cell.item = item;
//    
//    return cell;
//}


@end
